/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    gongwenkai
 * 创建时间:    2023-11-27
 */
package org.jeecg.modules.flow.callback.move.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryDocumentVO;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryVO;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * //TODO 【请填写类描述】
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-11-27
 */
@RequiredArgsConstructor
@Component
@Data
public class CommonMoveUtil {

    private final IWmsRepertoryService wmsRepertoryService;


    /**
     * 移库 实时库存
     */
    public void moveRepertory(String receiptId, String fromWmsBasicWarehouseId, List<WmsMoveReceiptMaterial> wmsMoveReceiptMaterialList) {
        if(StrUtil.isEmpty(fromWmsBasicWarehouseId)){
            throw new JeecgBootException("没有仓库信息");
        }
        // 减少即时库存
        List<RepertoryVO> reduceRepertoryList = wmsMoveReceiptMaterialList.stream().map(o -> {
            RepertoryVO repertoryVO = BeanUtil.copyProperties(o, RepertoryVO.class);
            repertoryVO.setWmsBasicWarehouseId(fromWmsBasicWarehouseId);
            repertoryVO.setWmsBasicWarehouseAreaId(o.getTransferFromAreaId());
            repertoryVO.setWmsBasicWarehouseAreaLocationId(o.getTransferFromLocationId());
            repertoryVO.setStockQuantity(o.getTransferQuantity());
            repertoryVO.setBatchCode(o.getBatchCode());
            repertoryVO.setStockQuantitySub(o.getAssistQuantity());
            return repertoryVO;
        }).collect(Collectors.toList());
        RepertoryDocumentVO repertoryDocumentVO = new RepertoryDocumentVO();
        repertoryDocumentVO.setInvoicesId(receiptId);
        repertoryDocumentVO.setDocumentType(3);
        repertoryDocumentVO.setRepertoryVOS(reduceRepertoryList);
        //扣除即时库存
        wmsRepertoryService.reduce(repertoryDocumentVO);

        //增加即时库存
        List<RepertoryVO> addRepertoryList = wmsMoveReceiptMaterialList.stream().map(o -> {
            RepertoryVO repertoryVO = BeanUtil.toBean(o, RepertoryVO.class);
            repertoryVO.setWmsBasicWarehouseId(fromWmsBasicWarehouseId);
            repertoryVO.setWmsBasicWarehouseAreaId(o.getTransferToAreaId());
            repertoryVO.setWmsBasicWarehouseAreaLocationId(o.getTransferToLocationId());
            repertoryVO.setWmsBasicMaterialId(o.getWmsBasicMaterialId());
            repertoryVO.setStockQuantity(o.getTransferQuantity());
            repertoryVO.setBatchCode(o.getBatchCode());
            repertoryVO.setStockQuantitySub(o.getAssistQuantity());
            if (StringUtils.isNotBlank(o.getRepertoryId())) {
                WmsRepertory repertory = wmsRepertoryService.getById(o.getRepertoryId());
                if (repertory != null) {
                    repertoryVO.setWmsBasicSupplierId(repertory.getWmsBasicSupplierId());
                    repertoryVO.setSupplier(repertory.getSupplier());
                }
            }
            return repertoryVO;
        }).collect(Collectors.toList());
        RepertoryDocumentVO repertoryDocumentVOS = new RepertoryDocumentVO();
        repertoryDocumentVOS.setDocumentType(3);
        repertoryDocumentVOS.setInvoicesId(receiptId);
        repertoryDocumentVOS.setRepertoryVOS(addRepertoryList);
        //增加库存
        wmsRepertoryService.add(repertoryDocumentVOS);
    }
}
